<?php
/**
 * @Author: EYong
 * @Date:   2018-04-27 14:04:32
 * @Last Modified by:   EYong
 * @Last Modified time: 2018-06-20 10:00:12
 */
namespace app\admin\controller;
use app\admin\model\Adminuser as AdminuserModel;
use app\admin\model\NoteM;
use app\admin\model\Signm;
use think\Db;
use think\facade\Config;
use think\facade\Log;

// extends controller
// class Admin extends Father
class Index extends Father {
	//显示后台主页
	public function index() {
		$usershow = '';
		switch (session('user_type')) {
		case 'admin':
			if (session('role_name')) {
				$usershow = session('role_name');
			} else {
				session('role_name', db('adm_role')->where('role_id', session('role_id'))->value('role_name'));
				$usershow = session('role_name');
			}
			break;
		case 'channel':
			if (session('channel_name') != '') {
				$usershow = session('channel_name') . ' | ID：' . session('relate_id') . ' | <span id="duanxinyue"></span>';
			}
			break;
		case 'salesman':
			$usershow = '业务员 ';
			break;
		default:
			$usershow = '用户';
			break;
		}

		$this->assign('usershow', $usershow);
		$this->assign('menu', session('menu'));
		return $this->fetch();
	}
	//主页
	public function welcome() {
		$type          = '';
		$channel_level = session('channel_level');
		$relate_id     = session('relate_id');

		$type        = '渠道账号 ';
		$bus_channel = Db::table('bus_channel')->where('channel_id', $relate_id)->find();
		$this->assign('channel_info', ['currency' => $bus_channel['currency']]);

		$configkj = [
			"SendNote/index" => ["id" => "192", "name" => "发送短信", "url" => url("SendNote/index"), "icon" => "layui-icon-cellphone"]
			, "Orders/index" => ["id" => "60", "name" => "发送查询", "url" => url("Orders/index"), "icon" => "layui-icon-search"]
			, "Recharge/index" => ["id" => "49", "name" => "充值管理", "url" => url("Recharge/index"), "icon" => "layui-icon-form"]
			, "Record/index" => ["id" => "50", "name" => "充值日志", "url" => url("Record/index"), "icon" => "layui-icon-list"],
			// , "OrderReport/index" => ["id" => "82", "name" => "日报表", "url" => url("OrderReport/index"), "icon" => "layui-icon-chart-screen"],
		];
		$ifauth = session('auth');

		if ($ifauth != '*') {
			foreach ($ifauth as $key => $value) {
				unset($ifauth[$key]);
				$ifauth[$value] = '';
			}
			$configkj = array_intersect_key($configkj, $ifauth);
		}

		if (session('user_type') == "channel") {
			if (in_array($channel_level, [3, 5])) {
				unset($configkj[2]); //删除充值管理
			} else {
				$configkj[2] = ["id" => "236", "name" => "短信充值", "url" => url("BalanceShift/index"), "icon" => "layui-icon-form"];
			}
		}

		// halt(session(''));

		$this->assign('configkj', $configkj);
		$this->assign('type', $type);

		return $this->fetch();
	}

	//404页面
	public function notFind() {
		return $this->fetch();
	}

	//修改密码
	// public function password(){
	//     return $this->fetch('change_pwd');
	// }
	public function changePwd() {
		$userId['user_id'] = trim(input('get.id'));
		// halt($user_id);
		if ($this->request->isPost()) {
			// halt($user_id);
			$data            = $this->request->post();
			$data['user_id'] = session('user_id');
			// halt($data);
			if (!is_numeric($data['user_id'])) {
				$result = [
					'code' => 1,
					'msg'  => '参数错误！~',
				];
				return json($result);die;
			}
			// 表单数据
			$userModel = new AdminuserModel();
			$res       = $userModel->checkUserInfo($data);
			if ($res) {
				$result = [
					'code' => 0,
					'msg'  => '修改成功！',
				];
				session(null);
			} else {
				$result = [
					'code' => 3,
					'msg'  => '原密码错误！',
				];
			}

			return json($result);die;
		}
		$this->assign('user_id', $userId);
		// dump($user_id);
		return $this->fetch();
	}

	/**
	 * 检查信息
	 * @Author   lf
	 * @DateTime 2018-07-03T14:27:13+0800
	 * @return   [type]                   [description]
	 */
	function getapplycount_com() {
		Log::close();return;
		$data = [];
		switch (session('user_type')) {
		case 'admin':
			if (input('tp') == 2) {

				$tody = substr(date('Ymd'), -6);

				//缓存，今天之前还有没处理的查一遍
				$tablename = cache('reviewResendOrder2020');

				if (empty($tablename) || !in_array($tody, $tablename)) {
					$tablename[] = $tody;
				}

				$data = $this->findReviewResend($tablename);

				cache('reviewResendOrder2020', $data[0]);

				$msgArr[] = $data[1];

				if (!$msgArr) {
					['code' => 0, 'msg' => []];
				}
				return json(['code' => 200, 'msg' => $msgArr]);
			}
			break;
		case 'channel':
			$relate_id     = session('relate_id');
			$channel_level = session('channel_level');
			if ($relate_id == '2013') {
				$data['sms_balance'] = -1;
				break;
			}
			if (!empty($relate_id)) {
				if (in_array($channel_level, [1, 4])) {

					$bus_channel         = Db::table('bus_channel')->where('channel_id', $relate_id)->find();
					$balance             = floor($bus_channel['oevrplus'] / $bus_channel['sms_price']);
					$data['sms_balance'] = $balance;
				} else {
					$data['sms_balance'] = Db::table('bus_channel')->where('channel_id', $relate_id)->value('sms_balance');
				}
			} else {
				$data['sms_balance'] = 0;
			}
			break;

		default:
			return;
			break;
		}

		return json($data);
	}

	public function findReviewResend($arrdate) {
		$msg_review      = '';
		$msg_resend      = '';
		$msg_temp_review = '';

		foreach ($arrdate as $k => $v) {
			// 查询这个待审的订单
			$review_order = Db::table('bus_review_order_' . $v)->alias('re')->field('cid,company_name')->join('bus_channel', 're.cid=bus_channel.channel_id')->where('re.status', 0)->group('cid')->column('company_name', 'cid');
			if ($review_order) {
				$msg_review .= '[时间' . '20' . $v . ']';
				foreach ($review_order as $key => $value) {
					$msg_review .= '(' . $key . ')' . $value . '；';
				}

			}

			// 查询这个待审的订单
			$resend_order = Db::table('bus_resend_order_' . $v)->alias('re')->field('cid,company_name')->join('bus_channel', 're.cid=bus_channel.channel_id')->where('check_status', 0)->group('cid')->column('company_name', 'cid');

			if ($resend_order) {
				$msg_resend .= '[<span style="color:#00795D">时间' . '20' . $v . '</span>]';
				foreach ($resend_order as $key => $value) {
					$msg_resend .= '(' . $key . ')' . $value . '；';
				}
			}

			// 查询这个彩信待审的订单
			$resend_order = Db::table('bus_channel_temp_review')->alias('re')->field('cid,company_name')->join('bus_channel', 're.cid=bus_channel.channel_id')->where('check_onoff', 0)->group('cid')->column('company_name', 'cid');

			if ($resend_order) {
				$msg_temp_review .= '[<span style="color:#00795D">时间' . '20' . $v . '</span>]';
				foreach ($resend_order as $key => $value) {
					$msg_temp_review .= '(' . $key . ')' . $value . '；';
				}
			}

			if (!$review_order && !$resend_order && !$msg_temp_review) {
				unset($arrdate[$k]);
			}
		}

		if ($msg_review) {
			$msg_review = '<span style="color:#f00">有新的渠道短信待审核：' . $msg_review . '</span>';
		}
		if ($msg_resend) {
			$msg_resend = '<span style="color:#c17f00">有新的渠道短信待审核：' . $msg_resend . '</span>';
		}
		if ($msg_temp_review) {
			$msg_temp_review = '<span style="color:#c17f00">有新的渠道彩信模板待审核：' . $msg_temp_review . '</span>';
		}

		return [$arrdate, $msg_review . $msg_resend];
	}

}
